<template>
  <div class="casecause-page">
    <d2-crud
      ref="d2Crud"
      :columns="columns"
      :data="dataTable"
      :options="options"
       @row-click="rowClick"
      index-row
      selection-row
      @selection-change="selectChange"
      @current-change="handleCurrentChange"
      :pagination="pagination"
      @pagination-current-change="paginationCurrentChange"
    >
      <div class="title" slot="header">
        <span class="text">法律法规列表</span>
        <head-btns :add="addDept" :mod="editDept" :del="delDept" />
      </div>

      <!-- <el-button slot="header" type="primary" style="margin-bottom: 5px" @click="addDept" icon="el-icon-circle-plus">添加</el-button>
      <el-button slot="header" type="success" style="margin-bottom: 5px" @click="editDept" icon="el-icon-edit">修改</el-button>
      <el-button slot="header" type="danger" style="margin-bottom: 5px" @click="delDept" icon="el-icon-delete">删除</el-button>-->
    </d2-crud>
    <edit-form :dept="dept" v-model="editFormVisible" @submit="getTableData" />
    <add-form v-model="addFormVisible" @submit="getTableData"></add-form>
  </div>
</template>
<script>
import bnslawApi from "@/api/system/bnslaw";
import editForm from "./editForm";
import addForm from "./addForm";
import HeadBtns from "@/views/comps/zhzf/head-btns";
import LegalItems from "@/views/system/bnslaw/legal-items";
import zhzfUtils from "@/assets/utils/zhzf-util";

export default {
  name: "bnslawPage",
  components: { editForm, addForm, HeadBtns },
  data() {
    return {
      columns: [
        {
          type: "expand",
          width: "20",
          component: {
            name: LegalItems
          }
        },
        {
          title: "法规标题",
          key: "strName",
          width: "180"
        },
        {
          title: "法规编码",
          key: "bcode",
          width: "50"
        },
        {
          title: "发布机关",
          key: "strOrgan"
        },
        {
          title: "发布文号",
          key: "strNumber"
        },
        {
          title: "发布时间",
          key: "dtmDate",
          formatter: (row, col, val) => {
            return zhzfUtils.formatDts(val, "YYYY-MM-DD HH:mm:ss");
          }
        },
        {
          title: "实施时间",
          key: "shiDate",
          formatter: (row, col, val) => {
            return zhzfUtils.formatDts(val, "YYYY-MM-DD HH:mm:ss");
          }
        },
        {
          title: "时效性",
          key: "status"
        }
      ],
      data: [],
      dataTable: [],
      options: {
        stripe: true,
        highlightCurrentRow: true
      },
      currentId: null,
      editFormVisible: false,
      dept: {},
      addFormVisible: false,
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      selected: []
    };
  },
  mounted() {
    this.getTableData();
  },
  methods: {
    getTableData() {
      bnslawApi.Page(this.pagination).then(res => {
        var data = res.data;
        this.data = data.rows;
        this.dataTable = data.rows;
        this.pagination.total = data.total;
      });
    },
    paginationCurrentChange(currentPage) {
      this.pagination.currentPage = currentPage;
      this.getTableData();
    },
    selectChange(selection) {
      //多选
      this.selected = selection;
    },
    rowClick(row) {
      this.$refs.d2Crud.$children[1].toggleRowSelection(row);
    },
    addDept() {
      this.addFormVisible = true;
    },
    handleCurrentChange(currentRow) {
      if (currentRow) {
        this.currentId = currentRow.bnslawId;
      }
    },
    totree(list) {
      //数组转为树形结构数据
      let obj = {};
      let result = [];
      //将数组中数据转为键值对结构 (这里的数组和obj会相互引用)
      list.map(el => {
        obj[el.id] = el;
      });
      for (let i = 0, len = list.length; i < len; i++) {
        let id = list[i].parentId;
        if (id == 0) {
          result.push(list[i]);
          continue;
        }
        if (obj[id] && obj[id].children) {
          obj[id].children.push(list[i]);
        } else {
          obj[id].children = [list[i]];
        }
      }
      console.log(result);
      return result;
    },
    editDept() {
      if (this.currentId) {
        for (let i = 0; i < this.data.length; i++) {
          if (this.data[i].bnslawId == this.currentId) {
            this.dept = this.data[i];
          }
        }
        this.editFormVisible = true;
      }
    },
    delDept() {
      this.$confirm("确认删除？", "确认信息", {
        distinguishCancelAndClose: true,
        confirmButtonText: "删除",
        cancelButtonText: "取消"
      }).then(() => {
        bnslawApi.del({ id: this.currentId }).then(() => {
          this.getTableData();
          this.currentId = null;
        });
      });
    }
  }
};
</script>

<style scoped lang="scss">
.el-row {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}
</style>
